<?php
/**
 * 自定义基类异常捕获
 * User: pengyixing
 * Date: 18/8/30
 * Time: 23:48
 */

namespace app\Lib\Exception;


use think\Exception;
use Throwable;

class CommonException extends Exception
{
    protected $exceptionLabel = 'CommonException';
    protected $errLevel; // 错误等级：数字越小等级越高

    public function __construct($message = "", $code = 0, Throwable $previous = null)
    {
        /** 保存错误相关位置及信息 */
        $trace = $this->getTrace();
        $debugData['file'] = $this->getFile();
        $debugData['line'] = $this->getLine();
        $debugData['controller'] = $trace[0]['class'];
        $debugData['function'] = $trace[0]['function'];
        $debugData['args'] = [
            '_GET' => $_GET,
            '_POST' => $_POST
        ];
        $this->setData($this->exceptionLabel, $debugData);
        $this->addLog();
        parent::__construct($message, $code, $previous);
    }

    /**
     * 获取错误等级
     * @return int
     */
    final public function getErrLevel()
    {
        return (int)$this->errLevel;

    }

    /**
     * 记录日志
     */
    private function addLog()
    {
        // TODO:自定义异常捕获日志记录
    }

}